$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'spo/orderRefund/list',
        datatype: "json",
        colModel: [			
        	{ label: '商品订单id', name: 'ordersId', width: 45, key: true ,hidden:true},
			{ label: '商品订单号', name: 'outOrderNo', width: 45},
			{ label: '退款金额', name: 'refundFee', width: 45},
			{ label: '状态flag', name: 'refundStatus', width: 45,hidden:true},
			{ label: '退款方式', name: 'collectionMethod', width: 45,
				formatter: function(value, options, row){
					return value === 0 ? 
						'微信' : 
						'支付宝';
				}	
			},
			{ label: '状态', name: 'refundStatusText', width: 45},
			{ label: '商品类型', name: 'gTypeName', width: 45},
			{ label: '商家名称', name: 'spName', width: 45},
			{ label: '商品名称', name: 'gName', width: 45},
			/*{ label: '商品单价', name: 'price', width: 45},
			{ label: '商品数量', name: 'goodsNum', width: 45},
			{ label: '商品总价', name: 'goodsTotalMoney', width: 45},*/
            { label: '下单用户', name: 'uName', width: 75 },
            { label: '时间', name: 'createTime', width: 90 ,
				formatter: function(value, options, row){
					
					var text="";
					if(row.refundStatus === 0){
						text='申请退款时间-'+ row.createTime;
					}else if(row.refundStatus === 1){
						text='审核不通过时间-'+ row.updateTime;
					}else if(row.refundStatus === 2){
						text='退款成功时间-'+ row.updateTime;
					}else if(row.refundStatus === 3){
						text='退款失败时间-'+ row.updateTime;
					}else if(row.refundStatus === 4){
						text='交易关闭时间-'+ row.updateTime;
					}
					
					return text;
				}
            
            },
            { label: '收货人', name: 'receiveName', width: 75 },
            { label: '收货人手机', name: 'receiveMobileNO', width: 75 },
            { label: '收货地址', name: 'receiveAddressFull' },
//			{ label: '交易订单号', name: 'outTradeNo', width: 75},
		 
        ],
		viewrecords: true,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
    

});

var setting = {
    data: {
        simpleData: {
            enable: true,
            idKey: "deptId",
            pIdKey: "parentId",
            rootPId: -1
        },
        
    	remindTypeList: [  
  	      { text: '点餐提醒', value: 0 },  
  	      { text: '吃药提醒', value: 1 },  
  	      { text: '康复活动提醒', value: 2 } 
  	    ],
        key: {
            url:"nourl"
        }
    }
};
var settingSick = {
	    data: {
	        simpleData: {
	            enable: true,
	            idKey: "deptId",
//	            pIdKey: "null",
	            rootPId: -1
	        },
	        key: {
	            url:"nourl"
	        }
	    }
	};
var ztree;
var ztreeSick;

var vm = new Vue({
	el:'#rrapp',
	data:{
		q:{
			bedNO: null,
		    refundStatus:0
		},
		showList: true,
		title:null,
		refundTypeList: [  
			  { text: '全部', value: -1 },  
		      { text: '待退款', value: 0 },  
//		      { text: '审核不通过', value: 1 },  
		      { text: '退款成功', value: 2 } 
		    ],
		pro:{
			/*deptId:null,
            deptName:null,
            sickAreaNO:null,
            sickAreaName:null*/
			 
		}
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.pro = {deptName:null, deptId:null, sickAreaNO:null,sickAreaName:null};
			vm.getDept();

		},
        getDept: function(){
            //加载组织树,非病区
            $.get(baseURL + "sys/dept/listNotSick", function(r){
                ztree = $.fn.zTree.init($("#deptTree"), setting, r);
                var node = ztree.getNodeByParam("deptId", vm.pro.deptId);
                if(node != null){
                    ztree.selectNode(node);
                    vm.pro.deptName = node[0].name;
                	vm.pro.deptId=node[0].deptId;
                   
				}
            })
        },
        getDeptUpdate: function(){
        	//加载组织树,非病区
        	$.get(baseURL + "sys/dept/listNotSick", function(r){
        		ztree = $.fn.zTree.init($("#deptTree"), setting, r);
        		var node = ztree.getNodeByParam("deptId", vm.pro.deptId);
        		if(node != null){
        			ztree.selectNode(node);
        			vm.pro.deptName = node.name;
        			vm.pro.deptId=node.deptId;
        			/*vm.pro.sickAreaNO=node.sickAreaNO;
        			vm.pro.sickAreaName=node.sickAreaName;*/
        			
        		}
        	})
        },
        getDeptSick: function(){
        	if(vm.pro.deptId==null){
        		alert("请先选择组织");
        		return;
        	}
            //加载组织树
            $.get(baseURL + "sys/dept/listSick/"+vm.pro.deptId+"", function(r){
            	ztreeSick = $.fn.zTree.init($("#deptTreeSick"), settingSick, r);
                var node = ztreeSick.getNodeByParam("deptId", vm.pro.sickAreaNO);
                if(node!=null){
                	ztreeSick.selectNode(node);
                	vm.pro.sickAreaNO=node[0].sickAreaNO;
                	vm.pro.sickAreaName=node[0].name;
                   
                }
                   
            })
        },
        getDeptSickUpdate: function(){
        	if(vm.pro.deptId==null){
        		alert("请先选择组织");
        		return;
        	}
        	//加载组织树
        	$.get(baseURL + "sys/dept/listSick/"+vm.pro.deptId+"", function(r){
        		ztreeSick = $.fn.zTree.init($("#deptTreeSick"), settingSick, r);
        		var node = ztreeSick.getNodeByParam("deptId", vm.pro.sickAreaNO);
        		if(node!=null){
        			ztreeSick.selectNode(node);
        			vm.pro.sickAreaNO=node.deptId;
        			vm.pro.sickAreaName=node.name;
        			
        		}
        		
        	})
        },
		update: function () {
			var selectedId = getSelectedRow();
			
			var rowData = $("#jqGrid").jqGrid("getRowData", selectedId);
			if(rowData.refundStatus==0){
				
				confirm('确定要退款选中的记录？', function(){
					$.ajax({
						type: "POST",
					    url: baseURL + "spo/deviceInit/delete",
	                    contentType: "application/json",
					    data: JSON.stringify(selectedId),
					    success: function(r){
							if(r.code == 0){
								alert('操作成功', function(){
	                                vm.reload();
								});
							}else{
								alert(r.msg);
							}
						}
					});
				});
			}else{
				alert("此记录必支持退款，请选择待退款状态记录");
				return;
				
			}
			
//			alert(id);
//			console.log(id);
//			if(id == null){
//				return ;
//			}
//			return ;
			
			/*vm.showList = false;
            vm.title = "修改";
			
			vm.getPro(id);*/
			
		},
		refund: function () {
			var id = getSelectedRow();
			if(id==null){
//        		alert("请先选择组织");
        		return;
        	}
			var rowData = $("#jqGrid").jqGrid("getRowData", id);
			if(rowData.refundStatus==0){
				confirm('确定要退款选中的记录？', function(){
					$.ajax({
						type: "POST",
					    url: baseURL + "spo/orderRefund/orderRefund",
	                    contentType: "application/json",
					    data: JSON.stringify(id),
					    success: function(r){
							if(r.code == 0){
								alert('操作成功', function(){
	                                vm.reload();
								});
							}else{
								alert(r.msg);
							}
						}
					});
				});
			}else{
				alert("此记录不支持退款，请选择待退款状态记录");
				return;
				
			}
			
//			alert(id);
//			console.log(id);
//			if(id == null){
//				return ;
//			}
//			return ;
			
			/*vm.showList = false;
            vm.title = "修改";
			
			vm.getPro(id);*/
			
		},
		del: function () {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "pro/deviceInit/delete",
                    contentType: "application/json",
				    data: JSON.stringify(ids),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(){
                                vm.reload();
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		saveOrUpdate: function () {
			var url = vm.pro.id_ == null ? "pro/deviceInit/save" : "pro/deviceInit/update";
			console.log(JSON.stringify(vm.pro));
			$.ajax({
				type: "POST",
			    url: baseURL + url,
                contentType: "application/json",
			    data: JSON.stringify(vm.pro),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		getPro: function(proId){
			$.get(baseURL + "pro/deviceInit/info/"+proId, function(r){
//				console.log(JSON.stringify(r.pro));
				vm.pro = r.pro;
				/*vm.pro.sickAreaName=r.pro.sickAreaName;
				vm.pro.sickAreaNO=r.pro.sickAreaNO;*/
                vm.getDeptUpdate();
                vm.getDeptSickUpdate();
			});
		},
	 
        deptTree: function(){
            layer.open({
                type: 1,
                offset: '50px',
                skin: 'layui-layer-molv',
                title: "选择组织",
                area: ['300px', '450px'],
                shade: 0,
                shadeClose: false,
                content: jQuery("#deptLayer"),
                btn: ['确定', '取消'],
                btn1: function (index) {
                    var node = ztree.getSelectedNodes();
                    //选择上级组织
                    	vm.pro.deptId = node[0].deptId;
                        vm.pro.deptName = node[0].name;
                        if(vm.pro.sickAreaNO!=null){
                        	vm.pro.sickAreaNO=null;
                        	  vm.pro.sickAreaName=null;
                        }

                    layer.close(index);
                }
            });
        },
        deptTreeSick: function(){
        	if(vm.pro.deptId==null){
        		alert("请先选择组织");
        		return;
        	}
        	vm.getDeptSickUpdate();
            layer.open({
                type: 1,
                offset: '50px',
                skin: 'layui-layer-molv',
                title: "选择病区",
                area: ['300px', '450px'],
                shade: 0,
                shadeClose: false,
                content: jQuery("#deptLayerSick"),
                btn: ['确定', '取消'],
                btn1: function (index) {
                    var node = ztreeSick.getSelectedNodes();
                    //选择上级组织
                    if(node.length>0){
                    	  vm.pro.sickAreaNO = node[0].deptId;
                          vm.pro.sickAreaName = node[0].name;
                    }
                      
                    layer.close(index);
                }
            });
        },
		reload: function () {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{ 
				 postData:{'gName': vm.q.gName,'refundStatus':vm.q.refundStatus},
                 page:page
            }).trigger("reloadGrid");
		},
		exportTpl: function (url) {
			 
			var type=url.substring(url.lastIndexOf("/")+1);
			jQuery.jdownload(baseURL+url+'?token=' + token,'type="'+type+'"','post' );
		 
		},
		downloadData: function (url) {
			jQuery("#down_div").removeClass("xhide");
   			jQuery("#over_div").removeClass("xhide");
			 url+='?isData=1';
			var type=url.substring(url.lastIndexOf("/")+1);
			jQuery.jdownload(baseURL+url+'&token=' + token,'type="'+type+'"','post' );
			jQuery("#down_div").addClass('xhide');
			jQuery("#over_div").addClass('xhide');
		}
	}
});